package net.oschina.renyong.query.generator;

import static java.util.stream.Collectors.joining;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

/**
 * mybatis plus特殊查询传递的参数
 * @param <T> 特殊查询对应的entity
 * @author Timothy
 */
public class QueryParam<T> {

	private QueryWrapper<T> queryWrapper;

	public QueryParam(QueryWrapper<T> queryWrapper) {
		this.queryWrapper = queryWrapper;
	}

	/**
	 * 获取查询条件的完整代码段，以where开头
	 * @return 查询条件的完整代码段，以where开头
	 */
	public String getWhereFull() {
		if (queryWrapper == null || queryWrapper.getExpression() == null
				|| queryWrapper.getExpression().getNormal().isEmpty()) {
			return "";
		}
		return "where " + queryWrapper.getExpression().getNormal().getSqlSegment();
	}

	/**
	 * 获取查询条件的部分代码段，以and开头
	 * @return 查询条件的部分代码段，以and开头
	 */
	public String getWhereStartByAnd() {
		if (queryWrapper == null || queryWrapper.getExpression() == null
				|| queryWrapper.getExpression().getNormal().isEmpty()) {
			return "";
		}
		return "and " + queryWrapper.getExpression().getNormal().getSqlSegment();
	}

	/**
	 * 获取查询条件的部分代码段，以or开头
	 * @return 查询条件的部分代码段，以or开头
	 */
	public String getWhereStartByOr() {
		if (queryWrapper == null || queryWrapper.getExpression() == null
				|| queryWrapper.getExpression().getNormal().isEmpty()) {
			return "";
		}
		return "or " + queryWrapper.getExpression().getNormal().getSqlSegment();
	}

	/**
	 * 获取group by的完整代码段，以group by开头
	 * @return group by的完整代码段，以group by开头
	 */
	public String getGroupByFull() {
		if (queryWrapper == null || queryWrapper.getExpression() == null
				|| queryWrapper.getExpression().getGroupBy().isEmpty()) {
			return "";
		}
		String sqlSegment = queryWrapper.getExpression().getGroupBy().stream().map(ISqlSegment::getSqlSegment)
				.collect(joining(StringPool.COMMA));
		return "group by " + sqlSegment;
	}

	/**
	 * 获取group by的部分代码段，以英文逗号开头
	 * @return group by的部分代码段，以英文逗号开头
	 */
	public String getGroupByStartByComma() {
		if (queryWrapper == null || queryWrapper.getExpression() == null
				|| queryWrapper.getExpression().getGroupBy().isEmpty()) {
			return "";
		}
		String sqlSegment = queryWrapper.getExpression().getGroupBy().stream().map(ISqlSegment::getSqlSegment)
				.collect(joining(StringPool.COMMA));
		return ", " + sqlSegment;
	}

	/**
	 * 获取having的完整代码段，以having开头
	 * @return having的完整代码段，以having开头
	 */
	public String getHavingFull() {
		if (queryWrapper == null || queryWrapper.getExpression() == null
				|| queryWrapper.getExpression().getHaving().isEmpty()) {
			return "";
		}
		String sqlSegment = queryWrapper.getExpression().getHaving().stream().map(ISqlSegment::getSqlSegment)
				.collect(joining(StringPool.SPACE));
		return "having " + sqlSegment;
	}

	/**
	 * 获取having的部分代码段，以and开头
	 * @return having的部分代码段，以and开头
	 */
	public String getHavingStartByAnd() {
		if (queryWrapper == null || queryWrapper.getExpression() == null
				|| queryWrapper.getExpression().getHaving().isEmpty()) {
			return "";
		}
		String sqlSegment = queryWrapper.getExpression().getHaving().stream().map(ISqlSegment::getSqlSegment)
				.collect(joining(StringPool.SPACE));
		return "and " + sqlSegment;
	}

	/**
	 * 获取having的部分代码段，以or开头
	 * @return having的部分代码段，以or开头
	 */
	public String getHavingStartByOr() {
		if (queryWrapper == null || queryWrapper.getExpression() == null
				|| queryWrapper.getExpression().getHaving().isEmpty()) {
			return "";
		}
		String sqlSegment = queryWrapper.getExpression().getHaving().stream().map(ISqlSegment::getSqlSegment)
				.collect(joining(StringPool.SPACE));
		return "or " + sqlSegment;
	}

	/**
	 * 获取order by的完整代码段，以order by开头
	 * @return order by的完整代码段，以order by开头
	 */
	public String getOrderByFull() {
		if (queryWrapper == null || queryWrapper.getExpression() == null
				|| queryWrapper.getExpression().getOrderBy().isEmpty()) {
			return "";
		}
		String sqlSegment = queryWrapper.getExpression().getOrderBy().stream().map(ISqlSegment::getSqlSegment)
				.collect(joining(StringPool.SPACE));
		return "order by " + sqlSegment;
	}

	/**
	 * 获取order by的部分代码段，以英文逗号开头
	 * @return order by的部分代码段，以英文逗号开头
	 */
	public String getOrderByStartByComma() {
		if (queryWrapper == null || queryWrapper.getExpression() == null
				|| queryWrapper.getExpression().getOrderBy().isEmpty()) {
			return "";
		}
		String sqlSegment = queryWrapper.getExpression().getOrderBy().stream().map(ISqlSegment::getSqlSegment)
				.collect(joining(StringPool.SPACE));
		return ", " + sqlSegment;
	}

	public Map<String, Object> getParamNameValuePairs() {
		return Optional.ofNullable(queryWrapper).map(QueryWrapper::getParamNameValuePairs).orElse(new HashMap<String, Object>());
	}
}
